package com.example.cofeed.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.cofeed.adapter.PushSettingAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseListActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Push;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class PushSettingActivity extends BaseListActivity<Push> {
    private PushSettingAdapter mPushSettingAdapter;

    @Override
    protected void bindRecyclerView(RecyclerView recLv) {
        setMainTitle("推送设置");
        recLv.setLayoutManager(new LinearLayoutManager(this));
        mPushSettingAdapter = new PushSettingAdapter();
        recLv.setAdapter(mPushSettingAdapter);
//        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        mPushSettingAdapter.setOnItemClickListener((adapter, view, position) -> {
            Push push = (Push) adapter.getData().get(position);
            ApiService.push_set(this, push.id, push.ispush == 0 ? 1 : 0, new DialogCallback<LzyResponse<Object>>(mContext) {
                @Override
                public void onSuccess(Response<LzyResponse<Object>> response) {
                    push.ispush = push.ispush == 0 ? 1 : 0;
                    mPushSettingAdapter.notifyDataSetChanged();
                }
            });
        });

    }

    @Override
    protected void loadData(int page, int pageCount) {
        ApiService.push_list_options(this, new DialogCallback<LzyResponse<List<Push>>>() {
            @Override
            public void onSuccess(Response<LzyResponse<List<Push>>> response) {
                onGetDataSuc(response.body().data);

            }
        });

    }

    @Override
    protected List<Push> getData() {
        return mPushSettingAdapter.getData();
    }

    @Override
    protected void bindData(boolean isRefresh, List<Push> list) {
        if (isRefresh){
            mPushSettingAdapter.setNewData(list);
        }else {
            mPushSettingAdapter.addData(list);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
